package net.sppan.blog.store;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sppan.blog.model.Deduction;
import net.sppan.blog.utils.ObjectUtils;

public class DeductionStore {

	private DeductionStore() {

	}

	public static final DeductionStore instance = new DeductionStore();

	private Map<String, Integer> agentDeductMapping = new HashMap<>();
	private Map<String, Integer> offerDeductMapping = new HashMap<>();

	public void init() {
		List<Deduction> ds = Deduction.dao.getAllList();
		if (ObjectUtils.isNotEmpty(ds)) {
			Map<String, Integer> _agentDeductMapping = new HashMap<>();
			Map<String, Integer> _offerDeductMapping = new HashMap<>();
			for (Deduction d : ds) {
				Integer userId = d.getUserId();
				Integer offerId = d.getOfferId();
				Integer ratio = d.getRatio();
				if (userId != null) {
					_agentDeductMapping.put(userId.toString(), ratio);
				}
				if (offerId != null) {
					_offerDeductMapping.put(offerId.toString(), ratio);
				}

			}
			agentDeductMapping = _agentDeductMapping;
			offerDeductMapping = _offerDeductMapping;

		}
	}

	/**
	 * 如果同时存在offer,agetn扣量,以offer粒度为准
	 * @param offerId
	 * @param agentId
	 * @return
	 */
	public Integer getDeductRatio(String offerId, String agentId) {
		Integer ratio = offerDeductMapping.get(offerId);
		if (ratio != null) {
			return ratio;
		} else {
			ratio = agentDeductMapping.get(agentId);
			return ratio;
		}
	}
}
